package cn.nawang.ebeim.test.task;

import cn.nawang.ebeim.test.constants.Config;
import cn.nawang.ebeim.test.service.TestData;
import cn.nawang.ebeim.test.transfer.FileInfo;
import cn.nawang.ebeim.test.transfer.TransferClient;
import cn.nawang.ebeim.test.transfer.TransferException;
import cn.nawang.ebeim.test.transfer.TransferReport;

import java.util.concurrent.TimeUnit;

/**
 * Created by GanJc on 2015-12-08 17:10
 */
public class DownloadTask implements Task {

    private FileInfo info ;

    public DownloadTask(FileInfo info) {
        this.info = info;
    }

    @Override
    public void execute() {
        try {
            TransferClient.getTransferClient().download(info, Config.SERVER_HOST, Config.SERVER_PORT,new TransferReport());
        } catch (TransferException e) {
            int tryCount = info.getTryCount();
            if(tryCount<Config.RETRY_TIMES){
                System.out.println("重试:" + tryCount);
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                tryCount ++ ;
                info.setTryCount(tryCount);
                throw new RuntimeException(e);
            }
        }
    }

}
